﻿@using NetCoreCMS.Models;
@using NetCoreCMS.Common;
@{
   Layout = "~/Areas/Admin/Views/Shared/_LayoutEdit.cshtml";
   ViewBag.Title = "Role_Edit";
   List<Navigation> list = ViewData["list"] as List<Navigation>;
   List<SelectListItem> roleList = ViewData["roleList"] as List<SelectListItem>;
   Manager_role m = ViewData["model"] as Manager_role;
}
@section HeaderContent{
   <script>
      $(function () {
         //初始化表单验证
         $("#form1").initValidform();
         //是否启用权限
         if ($("#ddlRoleType").find("option:selected").attr("value") == 1) {
            $(".border-table").find("input[type='checkbox']").prop("disabled", true);
         }
         $("#ddlRoleType").change(function () {
            if ($(this).find("option:selected").attr("value") == 1) {
               $(".border-table").find("input[type='checkbox']").prop("checked", false);
               $(".border-table").find("input[type='checkbox']").prop("disabled", true);
            } else {
               $(".border-table").find("input[type='checkbox']").prop("disabled", false);
            }
         });
         //权限全选
         $("input[name='checkAll']").click(function () {
            if ($(this).prop("checked") == true) {
               $(this).parent().siblings("td").find("input[type='checkbox']").prop("checked", true);
            } else {
               $(this).parent().siblings("td").find("input[type='checkbox']").prop("checked", false);
            }
         });
      });

      function submitSave() {
         var $row = $("#ltable tr:gt(0)");
         var json = { list: [] };
         $($row).each(function (i, item) {
            var name = $(item).find("td:eq(0)").find("label:eq(0)").text();
            var arr = new Array();
            var chks = $(item).find("input:checkbox");
            var i = 0;
            $(chks).each(function (_i, ctl) {
               if ($(ctl).prop("checked")) {
                  arr[i] = $(ctl).val();
                  i++;
               }
            });
            var obj = { name: name, action_type: arr };
            json.list.push(obj);
         });
         var action = $("#lbAction").text();
         var id = $("#lbId").text();
         var role_name = $("#txtRoleName").val();
         var role_type = $("#ddlRoleType").val();
         $.ajax({
            url: "SubmitSave?action=" + action + "&id=" + id + "&role_name=" + role_name + "&role_type=" + role_type,
            type: "POST",
            dataType: "html",
            data: "json=" + JSON.stringify(json),
            success: function (data) {
               eval(data);
            },
            error: function () {
               alert("error");
            }
         });
      }
   </script>
}
<form id="form1" method="post" action="SubmitSave?action=@ViewBag.Action&id=@ViewBag.Id">
<!--导航栏-->
<div class="location">
  <a href="../role_list/index" class="back"><i class="iconfont icon-up"></i><span>返回列表页</span></a>
  <a href="../center/index"><i class="iconfont icon-home"></i><span>首页</span></a>
  <i class="arrow iconfont icon-arrow-right"></i>
  <a href="../manager_list/index"><span>管理员</span></a>
  <i class="arrow iconfont icon-arrow-right"></i>
  <a href="../role_list/index"><span>管理角色</span></a>
  <i class="arrow iconfont icon-arrow-right"></i>
  <span>编辑角色</span>
  <div class="clipboard">
     <span><label id="clipMess"></label></span>
     <a href="javascript:;" class="copy" onclick="copy();"><i class="iconfont icon-copy"></i><span>复制</span></a>
     <a href="javascript:;" class="paste disable" onclick="paste();"><i class="iconfont icon-log"></i><span>粘贴</span></a>
  </div>
</div>
<div class="line10"></div>
<!--/导航栏-->

<!--内容-->
<div id="floatHead" class="content-tab-wrap">
  <div class="content-tab">
    <div class="content-tab-ul-wrap">
      <ul>
        <li><a class="selected" href="javascript:;">编辑角色信息</a></li>
      </ul>
    </div>
  </div>
</div>

<div class="tab-content">
  <dl>
    <dt>角色类型</dt>
    <dd>
      <label id="lbAction" style="display:none">@ViewBag.Action</label>
      <label id="lbId" style="display:none">@ViewBag.Id</label>
      <div class="rule-single-select">
        @Html.DropDownList("ddlRoleType", new SelectList(roleList, "Value", "Text", m.role_type), new { datatype = "*", errormsg = "请选择角色类型！", sucmsg = " " })
      </div>
    </dd>
  </dl>
  <dl>
    <dt>角色名称</dt>
    <dd>
       @Html.TextBox("txtRoleName", m.role_name, new { @class = "input normal", datatype = "*1-100", sucmsg = " " })
       <span class="Validform_checktip">*角色中文名称，100字符内</span>
    </dd>
  </dl>   
  <dl>
    <dt>管理权限</dt>
    <dd>
      <table border="0" cellspacing="0" cellpadding="0" class="border-table" width="98%" id="ltable">
        <thead>
          <tr>
            <th width="30%">导航名称</th>
            <th>权限分配</th>
            <th width="10%">全选</th>
          </tr>
        </thead>
        <tbody>
          @foreach (Navigation dr in list) {
          <tr>
            <td style="white-space:nowrap;word-break:break-all;overflow:hidden;">
              <label style="display:none">@dr.name</label>
              <label style="display:none">@dr.action_type</label>
              <label style="display:none">@dr.class_layer</label>
              @if (dr.class_layer == 1) {
                 <span class="folder-open"></span>
              }
              else {
                 <span style="display:inline-block;width:@(dr.class_layer - 2) * 24)px;\"></span><span class="folder-line"></span><span class="folder-open"></span>
              }
              @dr.title
            </td>
            <td>
              <span class="cbllist">
                 @{
              string[] ss = dr.action_type.Split(',');
                    }
                 @for (int i = 0; i < ss.Length; i++) {
                    if (Utils.ActionType().ContainsKey(ss[i])) {
                       Manager_role_value model = null;
                       if (m.manager_role_values != null) {
                           model = m.manager_role_values.Find(p => p.nav_name == dr.name && p.action_type == ss[i]);
                       }
                       if (model != null) {
                        <input type="checkbox" id="cbllist_@i" value="@ss[i]" checked="checked" />@Html.Raw("&nbsp;")<label for="cbllist_@i">@Utils.ActionType()[ss[i]]</label>
                       }
                       else {
                        <input type="checkbox" id="cbllist_@i" value="@ss[i]" />@Html.Raw("&nbsp;")<label for="cbllist_@i">@Utils.ActionType()[ss[i]]</label>
                       }
                    }
                 }
              </span>
            </td>
            <td align="center"><input type="checkbox" name="checkAll" /></td>
          </tr>
          }
        </tbody>
      </table>
    </dd>
  </dl>
</div>
<!--/内容-->

<!--工具栏-->
<div class="page-footer">
  <div class="btn-wrap">
    <input type="button" id="btnSubmit" value="提交保存" class="btn" onclick="submitSave();"/>
    <input name="btnReturn" type="button" value="返回上一页" class="btn yellow" onclick="javascript:history.back(-1);" />
  </div>
</div>
<!--/工具栏-->
</form>
